/*
 * ZENworks Notification Icon
 * Copyright (C) Author: Gangadhar Nagesh Metla (Novell, Inc.)
 * nmetla74@gmail.com 
 * Version 1.0
 * 1/3/2009
 * Filename JHyperLabel.java
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.novell.zenworks.agent.zicon.progress;
import java.awt.Cursor;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;

import com.novell.zenworks.agent.common.ICommonUIEnums.MB_ICON;
import com.novell.zenworks.agent.common.ICommonUIEnums.MB_RESPONSE;
import com.novell.zenworks.agent.common.ICommonUIEnums.MB_TYPE;
import com.novell.zenworks.agent.zbrowser.MessageBoxLauncher;
import com.novell.zenworks.agent.zicon.ZTrayIcon;
import com.novell.zenworks.zicon.common.LogUtil;

public class JHyperLabel extends JLabel 
{
    private static final long serialVersionUID = 1L;
    private String uid;
    private final Color NORMAL    = Color.BLUE;
    private final Color HOVER     = Color.RED;
    private final Color BACKGROUND =         Color.LIGHT_GRAY;
    private final Color WHITE = Color.WHITE;
    private Color defaultColor;
    private boolean keepSelected = false;

    public JHyperLabel(String text, String uid) 
    {
        super(text);
        this.uid = uid;
    }

    public JHyperLabel() 
    {
        setForeground(NORMAL);
        setBackground(WHITE);
        defaultColor = NORMAL;
        this.setSize((int)this.getPreferredSize().getWidth(),(int)this.getPreferredSize().getHeight());
        this.setOpaque(true);
        initListeners();
    }

    public void setUrl(String uid) 
    {
        this.uid = uid;
        setText(super.getText());
    }

    public void setText(String text) {
        super.setText(text);
    }

    public void paint(Graphics g) 
    {
        super.paint(g);
        g.drawLine(2, getHeight()-1, (int)getPreferredSize().getWidth()-2, getHeight()-1);
    }

    public void initListeners() 
    {

        addMouseListener( new MouseListener() 
        {

            public void mouseClicked(MouseEvent me) 
            {
                setForeground(NORMAL);
                defaultColor = NORMAL;
                try
                {
                    if(me.getComponent().getName().equals("removeLink"))
                        ZProgressDialog.getInstance().removePanel(uid, true);
                    else if(me.getComponent().getName().equals("postponeLink"))
                    {
                        ZProgressDialog.getInstance().postpone(uid);
                    }
                        
                } catch (Exception e)
                {
                    try
                    {
                        LogUtil.LogException(e.getMessage(), e);
                        if(e.getLocalizedMessage() != null)
                        {
                            MessageBoxLauncher.ShowMessageBoxHandler(e.getLocalizedMessage(), ZTrayIcon.rm.getString("PropDlgTitle") , MB_TYPE.MBT_OK, MB_ICON.MBI_ERROR, 30, false, MB_RESPONSE.MBR_OK);
                        }
                        else
                        {
                            MessageBoxLauncher.ShowMessageBoxHandler(ZTrayIcon.rm.getString("RemotingError"), ZTrayIcon.rm.getString("PropDlgTitle") , MB_TYPE.MBT_OK, MB_ICON.MBI_ERROR, 30, false, MB_RESPONSE.MBR_OK);
                        }
                    } catch (Exception e1)
                    {
                        LogUtil.LogException(e.getMessage(), e1);
                    }
                }
            }

            public void mouseReleased(MouseEvent me) 
            {

            }

            public void mousePressed(MouseEvent me) 
            {
                defaultColor = NORMAL;
            }

            public void mouseEntered(MouseEvent me) 
            {
                setForeground(HOVER);
                setBackground(BACKGROUND);
                Cursor cursor = getCursor();
                setCursor(cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }

            public void mouseExited(MouseEvent me) 
            {
                if(!keepSelected)
                {
                    setForeground(defaultColor);
                    setBackground(BACKGROUND);
                    Cursor cursor = getCursor();
                    setCursor(cursor.getDefaultCursor());
                }
            }
        });
    }
    
    public void setSelected(boolean select)
    {
        if(select)
        {
            keepSelected = true;
            setForeground(HOVER);
            setBackground(BACKGROUND);
            Cursor cursor = getCursor();
            setCursor(cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        }
        else
        {
            keepSelected = false;
            setForeground(defaultColor);
            setBackground(BACKGROUND);
            Cursor cursor = getCursor();
            setCursor(cursor.getDefaultCursor());
        }
    }

}

